package com.berserker4dream.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

/**
 * 
 * @author michael
 */
public class NetWorkUtils {
	/**
	 * First, you need to own Permission
	 *  	<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
	 * Then you could quickly get the networktype here.
	 * -1 means no network connection.
	 *  0 means other network , so you could try to get large data
	 *  1 means mobile network.
	 * @param context
	 * @return
	 */
	public static int getNetworkType(Context context) {
		ConnectivityManager cManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = cManager.getActiveNetworkInfo();
		if (null != info && info.isAvailable()) {
			LogUtils.Log("networktype:" + info.getType());
			if(isNetworkTypeMobile(info.getType())) {
				return 1;
			}
			return 0;
		}
		return -1;
	}

	/**
	 * Hiden in framework. So get it here. 
	 * It is really helpful to judge networktype.
	 * @param networkType
	 * @return
	 */
	public static boolean isNetworkTypeMobile(int networkType) {
		switch (networkType) {
		case TYPE_MOBILE:
		case TYPE_MOBILE_MMS:
		case TYPE_MOBILE_SUPL:
		case TYPE_MOBILE_DUN:
		case TYPE_MOBILE_HIPRI:
		case TYPE_MOBILE_FOTA:
		case TYPE_MOBILE_IMS:
		case TYPE_MOBILE_CBS:
			return true;
		default:
			return false;
		}
	}

	/**
	 * You could find this in android.net.ConnectivityManager.java
	 */
	public static final int TYPE_NONE = -1;
	public static final int TYPE_MOBILE = 0;
	public static final int TYPE_WIFI = 1;
	public static final int TYPE_MOBILE_MMS = 2;
	public static final int TYPE_MOBILE_SUPL = 3;
	public static final int TYPE_MOBILE_DUN = 4;
	public static final int TYPE_MOBILE_HIPRI = 5;
	public static final int TYPE_WIMAX = 6;
	public static final int TYPE_BLUETOOTH = 7;
	public static final int TYPE_DUMMY = 8;
	public static final int TYPE_ETHERNET = 9;
	public static final int TYPE_MOBILE_FOTA = 10;
	public static final int TYPE_MOBILE_IMS = 11;
	public static final int TYPE_MOBILE_CBS = 12;
	public static final int TYPE_WIFI_P2P = 13;

}
